﻿using System;
using System.Linq;
using System.Collections.Generic;
using Home.Mobilesales.Common;
using Home.MobileSales.Common.Entities;
using Home.MobileSales.Common.Models;
using OrderSales.Interface;



namespace OrderSales.Presenter
{
    class SelectGoodsPresenter
    {
        private ISelectGoodsView _view;
        private List<Good> allGoods;
        private List<Good> filteredGoods;
        private Order _order;

        public SelectGoodsPresenter(ISelectGoodsView view, Order order)
        {
            try
            {
                _view = view;
                _order = order;
                allGoods = DataManager.GetInstance().Goods.GetGoodsIncludeAll(Settings.GetInstance().VisibleNullGoods, order.SkladId);

                filteredGoods = allGoods;
                SetData();
                _view.ChangeFilter += FilteredData;
                _view.AddRecordOrder += AddRecordOrder;
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        private void SetData()
        {
            _view.ListGoods = filteredGoods;
            _view.ListGroups = filteredGoods.OrderBy(good => good.Group.Name).Select(good => good.Group.Name).Distinct().ToList();
            _view.ListMarks = filteredGoods.OrderBy(good => good.TradeMark.Name).Select(good => good.TradeMark.Name).Distinct().ToList();
        }

        private void FilteredData(object o, EventArgs e)
        {
            try
            {
                List<Good> temp = allGoods;
                if (_view.IsFilterGoup)
                {
                    temp = (temp.Where(good => good.Group.Name == _view.FilterGroup)).ToList();
                }
                if (_view.IsFilterMark)
                {
                    temp = (temp.Where(good => good.TradeMark.Name == _view.FilterMark)).ToList();
                }
                filteredGoods = temp;
                SetData();
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }

        void AddRecordOrder(object sender, EventArgs e)
        {
            try
            {
                OrderRecord record = new OrderRecord();
                record.Good = filteredGoods[_view.SelectGoodIndex];
                record.Price =
                    (double)(record.Good.GetType().GetProperty("Price" + _view.Price).GetValue(record.Good, null));
                record.Quantity = _view.Quantity;
                record.Amount = Math.Round(record.Price * record.Quantity, 2);
                if (Settings.GetInstance().CheckRemainingGoods)
                {
                    if (record.Quantity > record.Good.Remaining)
                        throw new OtherException("Недостатньо товару");
                    if (Settings.GetInstance().IsVanSales)
                    {
                        Remaining rem = record.Good.RemainingGoods;
                        rem.Quantity -= record.Quantity;
                        DataManager.GetInstance().Remainings.UpdateRemaining(rem);
                    }
                }
                DataManager.GetInstance().OrderRecords.InsertOrderRecord(record, _order.IdOrder);
            }
            catch (Exception ex)
            {
                ExceptionWork.ExeptionProcess(ex);
            }
        }
    }
}
